using System;
using System.Data;

namespace BRozklad.GAs
{
    class Population
    {
        public static DataSet rozkladDataSet;

        private static int kilkperson = 20;
        private Person[] persons;
        public Population(DataSet inrozklad)
        {
            persons = new Person[kilkperson];
            rozkladDataSet = inrozklad;
        }
        private bool NotNew(int index)
        {
            for (int i = 0; i < index; i++)
            {
                if (persons[i].Equals(persons[index]))
                    return true;
            }
            return false;
        }
        public void StartGenerate()
        {
            for (int i = 0; i < kilkperson; i++)
            {
                persons[i] = new Person();
                persons[i].StartGenerate(rozkladDataSet);
                if (NotNew(i))
                    i--;
            }
            Sort();
        }
        private void Sort()
        {
            int i, j, min;
            Person temppers;
            for (i = 0; i < kilkperson; i++)
            {
                min = i;
                for (j = i + 1; j < kilkperson; j++)
                    if (persons[min].Cost > persons[j].Cost)
                        min = j;
                if (min != i)
                {
                    temppers = persons[min];
                    persons[min] = persons[i];
                    persons[i] = temppers;
                }
            }
            int midcost = persons[kilkperson / 2 + 1].Cost;
            if (midcost < Configuration.mediancost)
                Configuration.mediancost = midcost;
            Configuration.goodcost = persons[0].Cost;
        }
        public void NextGenerate()
        {
            Random rand = Configuration.rand;
            int r;
            int halt = kilkperson / 2;
            for (int i = halt; i < kilkperson; i++)
            {
                r = rand.Next(halt);
                persons[i].NextGenerate(persons[r], rozkladDataSet);
                //                if (NotNew(i))
                if (!IsNewRozklad(i))
                    i--;
                else
                    Configuration.curentPeople++;
            }
            Sort();

        }


        private bool IsNewRozklad(int index)
        {
            bool newob = true;
            WorkWithDB wwdb = new WorkWithDB();
            wwdb.InsertToCur(persons[index].Rozklad);
            if (newob == true)
                return true;
            else
                return false;
        }


        /*dodatkovi*/
        public int First()
        {
            return persons[0].Cost;
        }
        public int Second()
        {
            return persons[1].Cost;
        }



        internal void MoveToRes()
        {
            Sort();
            persons[0].MoveToRes();
            Configuration.rescost = persons[0].Cost;
        }
    }
}